package net.yakiboo.adrums;

import java.util.List;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

public class DrawView extends View {
    
	Paint paint = new Paint();	
	Paint paint2 = new Paint();
	Paint paint3 = new Paint();
    List<Drum> drumsList = null;
    DrumKit dk = null;
    Canvas c = null;
    float pointer = 0;

    public void setPointer(float f){    	
    	this.pointer = f;
    	invalidate();
    }
    
    public DrawView(Context context, AttributeSet atts, int defStyle){
    	super(context, atts, defStyle);
    	paint.setAntiAlias(true);
    }
    
    public DrawView(Context context, AttributeSet atts){
    	super(context, atts);    
    	paint.setAntiAlias(true);
    }
    
    public DrawView(Context context) {
        super(context);
        paint.setAntiAlias(true);
    }
    
    public void setDrumKit(DrumKit drums){
    	dk = drums;
    	paint.setColor(Color.WHITE);    	
    	drumsList = drums.getDrums();
    	invalidate();
    }

    
    public float[] getLocation(Drum d, Canvas c){
    	float[] location = {0, 0};
    	location[0] = 50+(d.getPosition()/180)*(c.getWidth()-100);
    	location[1] = 100;
    	return location;
    }
    
    @Override
    public void onDraw(Canvas canvas) { 
    	if(this.c == null) this.c = canvas;    	
        if(drumsList != null && drumsList.size()>0){            	
        	for(Drum d: drumsList){        		
        		float[] loc = getLocation(d, canvas);
        		paint.setColor(Color.parseColor("#FF9100"));
        		canvas.drawCircle(loc[0], loc[1], 25, paint);
        		if(d.isSelected){
        			paint.setColor(Color.RED);
        		} else {
        			paint.setColor(Color.WHITE);
        		}
        		
        		canvas.drawCircle(loc[0], loc[1], 23, paint);
        		Log.w("adrums", "canvas=[" + canvas.getWidth() + "," + canvas.getHeight() + "], drum is at (" + loc[0] + ", " + loc[1] + ")");
        	}
        }
        paint2.setColor(Color.parseColor("#71C4FF"));
    	paint2.setAntiAlias(true);
    	canvas.drawCircle(50+(pointer/180)*(c.getWidth()-100) - 60, 100, 5, paint2); 
    	canvas.drawLine(50+(pointer/180)*(c.getWidth()-100) - 60, 100, canvas.getWidth()/2, canvas.getHeight()/2, paint2);
    	paint3.setColor(Color.WHITE);
    	paint3.setAntiAlias(true);
    	canvas.drawText("Mueva el dispositivo para cambiar de tambor", 40, canvas.getHeight() - canvas.getHeight()/3, paint3);
    	canvas.drawText("y agítelo para golpear", 100, canvas.getHeight() - canvas.getHeight()/3 + 20, paint3);
    }


    
}


